package com.ihr360.cnb.module.payroll.controller;

import com.ihr360.cnb.module.payroll.service.PayrollAggregateService;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.commons.lang.StringUtils;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.request.ApplicationApprovalRequest;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Eric
 */
@RestController
@RequestMapping("/payPeriod")
@DisableResponseAdvice
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.SALARY_TASK_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.SALARY_TASK_NAME,
        hasExtendsAbstractRestController = false)
public class PayPeriodAggregateController {

    @Autowired
    private PayrollAggregateService payrollAggregateService;

    /**
     * 薪资提请审批
     */
    @FunctionDefinition(functionName = "薪资提请审批", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "/submit/approval", httpMethod = "POST", notes = "薪资提请审批")
    @RequestMapping(value = "/submit/approval", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultInfo submitApproval(AuthUserInfo userInfo, @RequestBody ApplicationApprovalRequest applicationApprovalRequest) {
        ResultInfo resultInfo = new ResultInfo();
        if (applicationApprovalRequest == null || StringUtils.isEmpty(applicationApprovalRequest.getApplicationTitle())
                || StringUtils.isEmpty(applicationApprovalRequest.getType()) || applicationApprovalRequest.getId() == null) {
            throw new Ihr360Exception(null, "提请审批信息不可为空");
        }
        applicationApprovalRequest.setCompanyId(userInfo.getCompanyId());
        applicationApprovalRequest.setMobileNo(userInfo.getMobileNo());
        String message = payrollAggregateService.submitApproval(userInfo, applicationApprovalRequest);
        resultInfo.setCode(0);
        resultInfo.setMessage(message);
        return resultInfo;
    }

}
